package com.wiki.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wiki.entity.Ebook;
import com.wiki.mapper.EbookMapper;
import com.wiki.service.EbookService;
import com.wiki.utils.CopyUtil;
import com.wiki.utils.SnowFlake;
import com.wiki.utils.commonRequest.ebook.EbookQueryReq;
import com.wiki.utils.commonRequest.ebook.EbookSaveReq;
import com.wiki.utils.commonResponse.EbookQueryResp;
import com.wiki.utils.commonResponse.PageResp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 电子书 服务实现类
 * </p>
 *
 * @author 偷闰土的猹
 * @since 2022-01-04
 */
@Service
public class EbookServiceImpl extends ServiceImpl<EbookMapper, Ebook> implements EbookService {

    @Autowired
    private EbookMapper ebookMapper;

    @Autowired
    private SnowFlake snowFlake;

    @Override
    public PageResp<EbookQueryResp> listAll(EbookQueryReq req) {


        boolean flag = false;

        PageResp<EbookQueryResp> respList = new PageResp<>();
        if (req.getPage() != 0 && req.getSize() != 0) {
            PageHelper.startPage(req.getPage(), req.getSize());
        }

        QueryWrapper<Ebook> wrapper = new QueryWrapper<>();

        if (ObjectUtils.isNotEmpty(req.getCategory2Id())) {
            wrapper.like("category2_id", req.getCategory2Id());
            flag = true;
        }

        List<Ebook> ebooks;

        if (flag) {
            ebooks = ebookMapper.selectList(wrapper);
        } else {
            System.out.println("flag 为假");
            ebooks = ebookMapper.selectList(null);
        }


        PageInfo<Ebook> pageInfo = new PageInfo<>(ebooks);
        respList.setTotal(pageInfo.getTotal());
        List<EbookQueryResp> respList1 = CopyUtil.copyList(ebooks, EbookQueryResp.class);
        respList.setList(respList1);

        return respList;
    }

    @Override
    public void save(EbookSaveReq req) {
        Ebook copy = CopyUtil.copy(req, Ebook.class);
        if (ObjectUtils.isEmpty(req.getId())) {
            //新增
            Long id = snowFlake.nextId();
            copy.setId(id);
            ebookMapper.insert(copy);
        } else {
            //更新
            ebookMapper.updateById(copy);
        }
    }

    @Override
    public void delete(Long id) {
        int i = ebookMapper.deleteById(id);
        if (i > 0)
            System.out.println("删除了：" + id);
        else
            System.out.println("删除失败！id为：" + id);
    }

    @Override
    public PageResp<EbookQueryResp> queryLike(EbookQueryReq req) {
        PageResp<EbookQueryResp> respList = new PageResp<>();
        PageHelper.startPage(req.getPage(), req.getSize());
        QueryWrapper<Ebook> wrapper = new QueryWrapper<Ebook>().like("name", req.getName());
        List<Ebook> ebooks = ebookMapper.selectList(wrapper);
        PageInfo<Ebook> pageInfo = new PageInfo<>(ebooks);
        respList.setTotal(pageInfo.getTotal());
        List<EbookQueryResp> respList1 = CopyUtil.copyList(ebooks, EbookQueryResp.class);
        respList.setList(respList1);

        return respList;
    }


}
